package com.symaster.pdfcompose.frame.component.pdfpanel;

import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author yinmiao
 * @version 2024/4/28
 */
public class OptionBar extends JComponent implements ActionListener {
    final JButton upBtn;
    final JButton dnBtn;
    final JButton awUpBtn;
    final JButton awDnBtn;
    private final SourceItem sourceItem;

    public OptionBar(SourceItem sourceItem) {
        this.sourceItem = sourceItem;

        this.setLayout(new BorderLayout(1, 1));

        JPanel left = new JPanel(new BorderLayout(1, 1));

        // 移动至顶部按钮
        this.upBtn = new JButton(SwingUtil.loadIconEnabledCache("up.png", 13, 13));
        this.upBtn.addActionListener(this);
        this.upBtn.setToolTipText("移动至顶部");
        left.add(upBtn, BorderLayout.NORTH);

        // 移动至底部按钮
        this.dnBtn = new JButton(SwingUtil.loadIconEnabledCache("down.png", 13, 13));
        this.dnBtn.addActionListener(this);
        this.dnBtn.setToolTipText("移动至底部");
        left.add(dnBtn, BorderLayout.SOUTH);

        add(left, BorderLayout.WEST);

        JPanel right = new JPanel(new BorderLayout(1, 1));

        // 上移单行按钮
        this.awUpBtn = new JButton(SwingUtil.loadIconEnabledCache("arrowup.png", 13, 13));
        this.awUpBtn.addActionListener(this);
        this.awUpBtn.setToolTipText("向上移动");
        right.add(awUpBtn, BorderLayout.NORTH);

        // 下移单行按钮
        this.awDnBtn = new JButton(SwingUtil.loadIconEnabledCache("arrowdown.png", 13, 13));
        this.awDnBtn.addActionListener(this);
        this.awDnBtn.setToolTipText("向下移动");
        right.add(awDnBtn, BorderLayout.SOUTH);
        add(right, BorderLayout.EAST);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (sourceItem != null) {
            if (e.getSource() == upBtn) {
                sourceItem.toTopListener(e);
            } else if (e.getSource() == dnBtn) {
                sourceItem.toBottomListener(e);
            } else if (e.getSource() == awUpBtn) {
                sourceItem.upListener(e);
            } else if (e.getSource() == awDnBtn) {
                sourceItem.dnListener(e);
            }
        }
    }
}
